package com.zr.legalfactory.serevice.impl;

import com.zr.legalfactory.mapper.LegalFactoryMapper;
import com.zr.legalfactory.pojo.*;
import com.zr.legalfactory.serevice.LegalFactoryService;
import com.zr.util.AllRecords;
import com.zr.util.ResultBuildVo;
import com.zr.util.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.beans.Transient;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2019/6/15.
 */
@Service
@Slf4j
public class LegalFactorySServiceImpl implements LegalFactoryService {

    @Autowired
    private LegalFactoryMapper legalFactoryMapper;

    /**
     * 添加
     * @param legalFactoryAddVo
     * @return
     */
    @Override
    @Transient
    public ResultVo addLegalFactory(LegalFactoryAddVo legalFactoryAddVo) {

        //逻辑验证
        ResultVo assigment = assigment(legalFactoryAddVo);
        if (!assigment.isSuccess()){
            return assigment;
//            return ResultBuildVo.error(assigment.getErrorMessage(),assigment.getErrorCode());
        }

        //给数据库中的创建时间和修改时间等信息赋值
        Factory factory = new Factory();
        Date nowdate = new Date();
        factory.setCreateTime(nowdate);
        factory.setCreateName("小米");
        factory.setUpdateTime(nowdate);
        factory.setUpdateName("小米");
        //把左边的对象赋值给右边对象的数据
        //主要必须相同参数名称和相同的参数 类型才能自动进行赋值
        BeanUtils.copyProperties(legalFactoryAddVo,factory);
        legalFactoryMapper.addLegalFactory(factory);
        return ResultBuildVo.success(legalFactoryAddVo);
    }

    /**
     * 修改
     * @param legalFactoryUpdateVo
     * @return
     */
    @Override
    @Transient
    public ResultVo updateFactory(LegalFactoryUpdateVo legalFactoryUpdateVo) {
        //逻辑验证
        if(legalFactoryUpdateVo.getStatus() == 0){
            ResultVo assigment2 = assigment2(legalFactoryUpdateVo);
            if (!assigment2.isSuccess()){
                return assigment2;
    //            return ResultBuildVo.error(assigment.getErrorMessage(),assigment.getErrorCode());
            }

            //给数据库中的创建时间和修改时间等信息赋值
            Factory factory = new Factory();
            Date nowdate = new Date();
            factory.setCreateTime(nowdate);
            factory.setCreateName("小米");
            factory.setUpdateTime(nowdate);
            factory.setUpdateName("小米");
            //把左边的对象赋值给右边对象的数据
            //主要必须相同参数名称和相同的参数 类型才能自动进行赋值
            BeanUtils.copyProperties(legalFactoryUpdateVo,factory);
            legalFactoryMapper.updateFactory(factory);
            return ResultBuildVo.success(legalFactoryUpdateVo);
        }
        return ResultBuildVo.error("启用时不能修改","500");
    }

    /**
     * 查询
     * @param fid
     * @return
     */
    @Override
    public ResultVo queryFactoryByFid(Integer fid) {

        if (fid==null){
            return ResultBuildVo.error("查看是fid不能为空!","500");
        }
        Factory factory = legalFactoryMapper.queryByFid(fid);
        if (factory==null){
            return ResultBuildVo.error("查看对象不存在!","500");
        }
        return ResultBuildVo.success(factory);
    }

    /**
     * 分页
     * @param legalFactorySelectVo
     * @return
     */
    @Override
    public ResultVo<AllRecords> queryPage(LegalFactorySelectVo legalFactorySelectVo) {
        //查询数据
        List<Factory> factoryList = legalFactoryMapper.queryPage(legalFactorySelectVo);
        //查询数量
        int count = legalFactoryMapper.queryCount(legalFactorySelectVo);
        //把当前页、每页大小、总页数、总条数、数据统一放入到AllRecords返回
        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(legalFactorySelectVo.getPageIndex());
        allRecords.setPageSize(legalFactorySelectVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);
        allRecords.setDataList(factoryList);
        return ResultBuildVo.success(allRecords);
    }

    /**
     * 启用禁用
     * @param legalFactoryStatusVo
     * @return
     */
    @Override
    public ResultVo updateStatus(LegalFactoryStatusVo legalFactoryStatusVo) {
        if (legalFactoryStatusVo.getStatus() == 1){
            legalFactoryStatusVo.setStatus(0);
            //给数据库中的创建时间和修改时间等信息赋值
            Factory factory = new Factory();
            Date nowDate = new Date();
            factory.setUpdateTime(nowDate);
            factory.setUpdateName("小明");
            //把左边对象的数据赋值给右边对象的数据
            //注意必须相同参数名称和相同参数类型才能自动进行赋值
            BeanUtils.copyProperties(legalFactoryStatusVo,factory);
            legalFactoryMapper.updateStatus(factory);
            return ResultBuildVo.success(legalFactoryStatusVo);
        }
        if (legalFactoryStatusVo.getStatus() == 0){
            legalFactoryStatusVo.setStatus(1);
            //给数据库中的创建时间和修改时间等信息赋值
            Factory factory = new Factory();
            Date nowDate = new Date();
            factory.setUpdateTime(nowDate);
            factory.setUpdateName("小明");
            //把左边对象的数据赋值给右边对象的数据
            //注意必须相同参数名称和相同参数类型才能自动进行赋值
            BeanUtils.copyProperties(legalFactoryStatusVo,factory);
            legalFactoryMapper.updateStatus(factory);
            return ResultBuildVo.success(legalFactoryStatusVo);
        }
        return ResultBuildVo.success(legalFactoryStatusVo);
    }

    private ResultVo assigment(LegalFactoryAddVo legalFactoryAddVo) {
        List<Factory> legalFactoryFcode = legalFactoryMapper.queryByFcode(legalFactoryAddVo.getFcode());
        if (!CollectionUtils.isEmpty(legalFactoryFcode)){
            return ResultBuildVo.error("已经存在编码！","500");
        }
        return ResultBuildVo.success(legalFactoryAddVo);
    }

    private ResultVo assigment2(LegalFactoryUpdateVo legalFactoryUpdateVo) {
        List<Factory> legalFactoryContent = legalFactoryMapper.queryByContent(legalFactoryUpdateVo.getContent());
        if (!CollectionUtils.isEmpty(legalFactoryContent)){
            return ResultBuildVo.error("描述已经存在！","500");
        }
        return ResultBuildVo.success(legalFactoryUpdateVo);
    }
}
